package com.avic.rong.search.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.search.fragment.SearchAllFragment;
import com.avic.rong.search.fragment.SearchTransformFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchArticleFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchBiaoZhunFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchGoodsFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchPeiXunFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchWenDaFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchMainActivity extends BaseActivity {


    @BindView(R.id.tbLayout)
    TabLayout tbLayout;
    @BindView(R.id.view_pager)
    ViewPager view_pager;
    @BindView(R.id.tv_filter)
    TextView tv_filter;//筛选


    private List<String> titleList = new ArrayList<>();
    private List<Fragment> fragmentList = new ArrayList<>();


    @Override
    protected int getContentViewId() {
        return R.layout.activity_search_main;
    }


    @Override
    protected void initView() {
        ButterKnife.bind(this);

        titleList.add("全部");
        titleList.add("标准");
        titleList.add("培训");
        titleList.add("商品");
        titleList.add("文章");
        titleList.add("问答");
        titleList.add("成果");
//        fragmentList.add(SearchAllFragment.newInstance(this));
//        fragmentList.add(new SearchBiaoZhunFragment());
//        fragmentList.add(new SearchPeiXunFragment());
//        fragmentList.add(new SearchGoodsFragment());
//        fragmentList.add(new SearchArticleFragment());
//        fragmentList.add(new SearchWenDaFragment());

        tbLayout.setupWithViewPager(view_pager);
        view_pager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
        view_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                currentIndex = i;
                if ("标准".equals(titleList.get(i))){
                    tv_filter.setVisibility(View.GONE);
                }else if("商品".equals(titleList.get(i))){
                    tv_filter.setVisibility(View.GONE);
                }else{
                    tv_filter.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    private int currentIndex = 0;
    Fragment currentFragment = null;
    class MyFragmentPagerAdapter extends FragmentPagerAdapter {


        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            if (!(fragmentList.size() > position && fragmentList.get(position)!= null)){
                if (position == 0){
                    currentFragment = SearchAllFragment.newInstance(SearchMainActivity.this,"");
                }else if(position == 1){
                    currentFragment = new SearchBiaoZhunFragment();
                }else if(position == 2){
                    currentFragment = new SearchPeiXunFragment();
                }else if(position == 3){
                    currentFragment = new SearchGoodsFragment();
                }else if(position == 4){
                    currentFragment = new SearchArticleFragment();
                }else if(position == 5){
                    currentFragment = new SearchWenDaFragment();
                }else if(position == 6){
                    currentFragment = SearchTransformFragment.getInstance(context);
                }
                fragmentList.add(currentFragment);
            }
            return currentFragment;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }

        @Override
        public int getCount() {
            return titleList.size();
        }
    }







    public static Intent getIntent(Context context) {
        Intent intent = new Intent(context, SearchMainActivity.class);
        return intent;
    }


    @Override
    protected void addListener() {

    }


}
